<template>
	<view class="content">
		<u-navbar title="员工登录" :placeholder="true" left-icon-size="32rpx" bg-color="#FF594D" :autoBack="true"
			titleStyle="color:#fff;font-size:32rpx" height="88rpx" leftIconColor="#fff">
		</u-navbar>
		<view class="login_from">
			<u--form labelPosition="left" labelSize="26rpx" :model="employee" ref="uForm">
				<u-form-item label="账号" prop="empInfo.username" :customStyle="formCustomStyle" borderBottom ref="item1">
					<u--input v-model="employee.empInfo.username" placeholder="请输入账号" border="none"></u--input>
				</u-form-item>
				<u-form-item label="密码" prop="empInfo.password" :customStyle="formCustomStyle" borderBottom ref="item2">
					<u--input v-model="employee.empInfo.password" placeholder="请输入密码" type="password" border="none"></u--input>
				</u-form-item>
				<u-form-item>
					<u-button text="登录" @click="login" :customStyle="formCustomStyle"></u-button>
				</u-form-item>
			</u--form>
		</view>
	</view>
</template>

<script>
	import {
		empLogin,
		getEmployeeInfo
	} from '../../../api/employee.js'

	export default {
		data() {
			return {
				employee: {
					empInfo: {
						username: '',
						password: '',
					},
				},
				formCustomStyle: {
					'font-size': '26rpx'
				}
			}
		},
		methods: {
			async login() {
				if (this.employee.empInfo.username != '' && this.employee.empInfo.password != '') {
					console.log(this.employee.empInfo);
					const res = await empLogin(this.employee.empInfo)
					console.log(res);
					if (res.code != 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						return
					}
					uni.setStorageSync('employee', res.token)
					this.$store.commit('setEmployee', res.token)
					this.$store.commit('setEmployeeIsLogin', true)
					const _this = this
					uni.showToast({
						title: '登录成功',
						success() {
							(async function() {
								const res = await getEmployeeInfo()
								console.log(res);
								if (res.code == 200) {
									uni.setStorageSync('employeeInfo', JSON.stringify(res.user))
									_this.$store.commit('setEmployeeInfo', res.user)
								}
							})();
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/mine/mine'
								})
							}, 500)
						}
					})

					return
				}
				uni.showToast({
					title: '账号密码必填',
					icon: 'error'
				})

			}
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #fff;
		min-height: 100vh;

		.login_from {
			// margin: ;
			padding: 20rpx 30rpx;
		}
	}
</style>